<!--
 * @Description:
 * @Author: zhaolei
 * @Date: 2023-04-10 18:16:02
-->
<template>
  <div>
    <avue-crud
      ref="crud"
      v-model="form"
      :page.sync="page"
      :data="tableData"
      :table-loading="tableLoading"
      :option="tableOption"
      @on-load="getList"
      @search-change="searchChange"
      @search-reset="searchReset"
    >
      <template
        slot="menu"
        slot-scope="scope"
      >
        <el-button
          type="text"
          size="small"
          icon="el-icon-delete"
          @click="view(scope.row, scope.index)"
        >
          预约详情
        </el-button>
      </template>
    </avue-crud>
    <appointmentPopup
      :id="id"
      v-model="detailVisible"
      :pet-id="petId"
    ></appointmentPopup>
  </div>
</template>

<script>
import { fetchListByPetId } from '@/api/core/appointment';
import { tableOption } from '@/const/crud/pet/appointment';
import { mapGetters } from 'vuex';
import crudMixins from '@/mixins/crud';
import appointmentPopup from './appointmentPopup.vue';
export default {
  name: 'appointmentTab',
  components: {
    appointmentPopup,
  },
  mixins: [crudMixins],
  props: {
    petId: {
      type: [Object, String],
    },
  },
  data() {
    return {
      detailVisible: false,
      form: {},
      tableOption: tableOption(),
      id: null,
    };
  },
  computed: {
    ...mapGetters(['permissions']),
  },
  watch: {
    petId: {
      immediate: true,
      handler(val) {
        if (val) {
          this.getList();
        }
      },
    },
  },
  methods: {
    view(row) {
      this.id = row.id;
      this.detailVisible = true;
    },
    // 列表查询
    getList(page, refresh) {
      if (refresh) {
        this.page.current = 1;
        this.page.currentPage = 1;
      }
      this.tableLoading = true;
      const timeObj = {};
      if (this.searchForm.reserveTime && this.searchForm.reserveTime.length) {
        timeObj.reserveStartDate = this.searchForm.reserveTime[0];
        timeObj.reserveEndDate = this.searchForm.reserveTime[1];
        // delete this.searchForm.reserveTime;
      }
      if (this.searchForm.createTime && this.searchForm.createTime.length) {
        timeObj.applyStartDate = this.searchForm.createTime[0];
        timeObj.applyEndDate = this.searchForm.createTime[1];
        // delete this.searchForm.createTime;
      }
      fetchListByPetId(
        Object.assign(
          {
            current: this.page.currentPage,
            size: this.page.pageSize,
            petId: this.petId,
          },
          this.searchForm,
          timeObj
        )
      )
        .then((response) => {
          this.tableData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.tableLoading = false;
          this.tableData.forEach((el) => {
            el.petType = [el.typeCode, el.varietyCode];
            el.avatar = JSON.parse(el.avatar)[0].url;
            // el.avatar =
            //   'https://img.alicdn.com/tfs/TB1v28TC8v0gK0jSZKbXXbK2FXa-1880-640.jpg';
          });
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep {
  .avue-crud__header {
    height: 0 !important;
    min-height: 0 !important;
  }
}
</style>
